/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wargame.dao;

import wargame.entity.*;
import connectionmanager.DB;

/**
 *
 * @author Clarence
 */
public class GlobalDAO {

    private static boolean hasEntry() {
        String sql = "SELECT * FROM `global`";
        DB db = new DB();
        db.connect();
        db.query(sql);
        boolean hasEntry = false;
        if (db.next()) {
            hasEntry = true;
        }
        db.close();
        return hasEntry;
    }

    public static boolean isGamePaused() {
        String sql = "SELECT `pause_game` FROM `global`";
        DB db = new DB();
        db.connect();
        db.query(sql);
        boolean isGamePaused = true;
        if (db.next()) {
            if (db.getInt("pause_game") == 0) {
                isGamePaused = false;
            }
        }
        db.close();
        return isGamePaused;
    }

    public static void pauseGame(boolean pause) {
        insertIfNoEntry();

        int sql_value = 0;
        if (pause) {
            sql_value = 1;
        } else {
            sql_value = 0;
        }

        String sql = "UPDATE global SET pause_game = '" + sql_value + "';";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }

    private static void insertIfNoEntry() {
        if (!GlobalDAO.hasEntry()) { // Trying to ensure that database only should have 1 entry
            String sql = "INSERT INTO global (`pause_game`) VALUES ('1');";
            DB db = new DB();
            db.connect();
            db.execute(sql);
            db.close();
        }
    }
}
